package com.soup.view;

import com.soup.dao.BookDao;
import com.soup.dao.BorrowLogDao;
import com.soup.domain.Book;
import com.soup.domain.BorrowLog;
import com.soup.view.component.Context;
import com.soup.view.component.MassageTip;
import com.soup.view.value.LoginValue;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class ReturnFrame extends Context implements ActionListener {

    public JTextField name = new JTextField(10);
    public JTextField author = new JTextField(10);
    public JTextField publish = new JTextField(10);
    public JTextField have = new JTextField(10);
    public JTextField hot = new JTextField(10);

    public JTextField time = new JTextField(10);

    private JButton left = new JButton("<");
    private JButton right = new JButton(">");

    private JButton give = new JButton("还书");

    private BorrowLogDao logDao = new BorrowLogDao();
    private BookDao dao = new BookDao();

    List<Book> cache = new ArrayList<>();
    private int indexOfCache = 0;
    List<BorrowLog> borrowLogs = null;


    public ReturnFrame(int width, int height) {
        super(width, height);


        name.setBounds(60,50,width-120,40);
        author.setBounds(60,100,width-120,40);
        publish.setBounds(60,150,width-120,40);
        have.setBounds(60,200,width-120,40);
        hot.setBounds(60,250,width-120,40);
        time.setBounds(60,300,width-120,40);

        this.add(name, "名称", 100);
        this.add(author, "作者", 100);
        this.add(publish, "出版社", 100);
        this.add(have, "库存", 100);
        this.add(hot, "热度", 100);
        this.add(time, "热度", 100);

        left.setBounds(0,(height-40)/2,40,40);
        this.add(left,20);
        right.setBounds(width-40,(height-40)/2,40,40);
        this.add(right,20);

        give.setBounds(60,350,width-120,40);
        this.add(give,20);

        borrowLogs = logDao.queryOrder(LoginValue.id);
        Map<String,Object> map = new HashMap<>();
        borrowLogs.forEach(log->{
            map.clear();
            map.put("name",log.getName());
            List<Book> query = dao.query(map);
            cache.add(query.get(0));
        });
        setBook(cache.get(indexOfCache));
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        time.setText(dateFormat.format(new Date(borrowLogs.get(indexOfCache).getDat())));

        left.addActionListener(this);
        right.addActionListener(this);

        give.addActionListener(this);
    }

    public void setBook(Book book){
        name.setText(book.getName());
        author.setText(book.getAuthor());
        publish.setText(book.getPublish());
        have.setText(book.getHave()+"");
        hot.setText(book.getHot()+"");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (cache != null){
            if (e.getSource() == left){
                if (indexOfCache > 0){
                    indexOfCache --;
                    setBook(cache.get(indexOfCache));
                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    time.setText(dateFormat.format(new Date(borrowLogs.get(indexOfCache).getDat())));
                }else{
                    MassageTip.showMassage(null,"前面再没有了");
                }
            }else if (e.getSource() == right){
                if (indexOfCache < cache.size()-1){
                    indexOfCache ++;
                    setBook(cache.get(indexOfCache));
                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    time.setText(dateFormat.format(new Date(borrowLogs.get(indexOfCache).getDat())));
                }else {
                    MassageTip.showMassage(null,"后面再没有了");
                }
            }
        }

        if (e.getSource() == give){

            synchronized (this){

                BorrowLog log = new BorrowLog();
                log.setId(LoginValue.id);
                log.setName(name.getText());
                log.setDat(borrowLogs.get(indexOfCache).getDat());
                int delete = logDao.delete(log);

                Book book = new Book();
                book.setName(name.getName());
                book.setAuthor(author.getText());
                book.setHot(Integer.parseInt(hot.getText()));
                book.setHave(Integer.parseInt(have.getText())+1);
                book.setPublish(publish.getText());
                int update = dao.update(book);

                if (delete+update == 2){
                    MassageTip.showMassage(null,"还书成功");
                }else{
                    MassageTip.showMassage(null,"还书失败");
                }
            }

        }
    }
}
